<?php

namespace backend\components;

use Yii;
use backend\models;


class PreBehavior extends \yii\base\ActionFilter
{
    public function beforeAction ($action)
    {
        
       
        //关闭 csrf
        if(in_array(Yii::$app->controller->action->id, \Yii::$app->params['nocsrf_action'])){
            //echo Yii::$app->controller->action->id;
           
            $action->controller->enableCsrfValidation = false;
               
        }
        //设置后台主题 
        $this->setStyle();
        //加载用户在后台设置的配置 到 params
        $this->loadConfig();
        
        return true;
    }
   
    /**
     * 
     *   加载用户在后台设置的配置 到 params
     * 
     * **/
    
    public function loadConfig(){
        
      
        $configs = \backend\controllers\ConfigController::getConfigList();
        
        Yii::$app->params = array_merge(Yii::$app->params,$configs);
        
    }
    /**
     * 
     *    设置主题 
     * 
     * 
     * **/
    public function setStyle(){
        
        //修改主题 
        $model = new models\Config();
        $skin  = $model->getConfig("COLOR_STYLE");
        $skin = $skin['value'];
        $colors = Array(
             "skin-blue",
             'skin-black',
             'skin-red',
             'skin-yellow',
             'skin-purple',
             'skin-green',
             'skin-blue-light',
             'skin-black-light',
             'skin-red-light',
             'skin-red-light',
             'skin-purple-light',
             'skin-green-light',
        );
        
        $skin = in_array($skin, $colors)?$skin:"skin-red";
        
        Yii::$app->components['assetManager']['bundles']['dmstr\web\AdminLteAsset']['skin'];
        $componets = Yii::$app->getComponents();
        $componets['assetManager']['bundles']['dmstr\web\AdminLteAsset']['skin'] = $skin;
        
        Yii::$app->setComponents($componets);
        
        
    }
    
}